package com.itheima.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.CheckGroupService;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupVS;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    /*分页查询检查组列表*/
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList = checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),checkGroupList);
    }

    /*添加检查组*/
    @Override
    public void addCheckGroup(CheckGroup checkGroup) {
        /*1、添加检查组*/
        checkGroupDao.addCheckGroup(checkGroup);
        /*1、将检查组与检查项关联起来  此时的 checkGroup 中的id已经有值了*/
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();

        addCheckGroupIdAndcheckItemId(checkGroup, checkitemIds);
    }

    //将检查组与检查项关联起来
    private void addCheckGroupIdAndcheckItemId(CheckGroup checkGroup, List<Integer> checkitemIds) {
        if (CollectionUtils.isNotEmpty(checkitemIds)){
            /*将检查组与检查项关联的id数据放入List集合中，然后与数据库进行连接，一次性导入*/
            List<Map<String,Integer>> mapList =  new ArrayList<>();

            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroup.getId());
                map.put("checkitem_id",checkitemId);
                mapList.add(map);
            }

            checkGroupDao.addCheckGroupIdAndcheckItemId(mapList);
        }
    }

    /*编辑检查组*/
    @Override
    public void editCheckGroup(CheckGroup checkGroup) {
        //更改检查组信息
        checkGroupDao.updateCheckGroup(checkGroup);
        /*查询检查组包含的检查项的id列表*/
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
        //删除检查组与检查项的关联
        checkGroupDao.deleteCheckGroupIdAndcheckItemId(checkGroup.getId());
        //添加检查组与检查项的关联
        addCheckGroupIdAndcheckItemId(checkGroup, checkitemIds);
    }

    /*我们查询检查组信息，所有检查项信息，关联的检查项的id*/
    @Override
    public CheckGroupVS queryCheckGroup(Integer id) {
        CheckGroup checkGroup = checkGroupDao.queryCheckGroup(id);
        List<CheckItem> checkItemList = checkItemDao.queryAll();
        List<Integer> checkItemIds = checkGroupDao.queryCheckGroupIdAndcheckItemId(id);
        CheckGroupVS checkGroupVS = new CheckGroupVS(checkGroup,checkItemList,checkItemIds);

        return checkGroupVS;
    }

    /*删除检查组*/
    @Override
    public void deleteCheckGroup(Integer id) {
        /*先删关联*/
        checkGroupDao.deleteCheckGroupIdAndcheckItemId(id);
        /*再删检查组*/
        checkGroupDao.deleteCheckGroup(id);
    }


    /*查询所有检查组信息*/
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
